﻿@model Nop.Admin.Models.Advert.AdvertisementListModel
@using Telerik.Web.Mvc.UI
@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    
    //page title
    ViewBag.Title = @T("Admin.Advert.Advertisement.Manage").Text;
}
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-catalog.png")" alt="" />
        @T("Admin.Advert.Advertisement.Manage") @Html.ActionLink("(" + T("Admin.Advert.Advertisement.SwitchToTreeView") + ")", "Tree")
    </div>
    <div class="options">
        <a href="@Url.Action("Create")" class="t-button">@T("Admin.Common.AddNew")</a> <a href="@Url.Action("ExportXml")" class="t-button">@T("Admin.Common.ExportToXml")</a>
    </div>
</div>
<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchAdvertName):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => Model.SearchAdvertName)
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <button type="submit" id="btnSearch" class="t-button">@T("Admin.Common.Search")</button>
        </td>
    </tr>
</table>
<p></p>
<table class="adminContent">
    <tr>
        <td>
            @(Html.Telerik().Grid<Nop.Admin.Models.Advert.AdvertisementModel>(Model.Advertisement.Data)
                    .Name("advert-grid")
                    .ClientEvents(events => events
                        .OnDataBinding("onDataBinding"))
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.Name);
                        columns.Bound(x => x.StartTime);
                        columns.Bound(x => x.EndTime);
                        columns.Bound(x => x.Id)
                            .Width(50)
                            .Centered()
                            .Template( x => (x.StartTime<=DateTime.Now? Html.Raw("已生效"):Html.ActionLink(T("Admin.Common.Edit").Text, "Edit", new { id = x.Id })))
                            .ClientTemplate("<a href=\"Edit/<#= Id #>\">" + T("Admin.Common.Edit").Text + "</a>")
                            .Title(T("Admin.Common.Edit").Text);
                    })
                    .Pageable(settings => settings.Total(Model.Advertisement.Total).PageSize(gridPageSize).Position(GridPagerPosition.Both))
                            .DataBinding(dataBinding => dataBinding.Ajax().Select("List", "Advert"))
                    .EnableCustomBinding(true))
        </td>
    </tr>
</table>

<script type="text/javascript">
    $(document).ready(function () {
        //search button
        $('#btnSearch').click(function () {
            //search
            var grid = $('#advert-grid').data('tGrid');
            grid.currentPage = 1; //new search. Set page size to 1
            grid.ajaxRequest();
            return false;
        });

        $("#@Html.FieldIdFor(model => model.SearchAdvertName)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#btnSearch").click();
                return false;
            }
        });
    });

    function onDataBinding(e) {
        var searchModel = {
            SearchCategoryName: $('#@Html.FieldIdFor(model => model.SearchAdvertName)').val()
        };
        e.data = searchModel;
    }
</script>